package com.xin.utils
{
import com.xin.vo.KeyValue;
/**
 * url地址数据处理
 * @author daichangxin
 */
public class URLUtils
{
	private var _url:String;
	private var _fileName:String;
	private var _fileType:String;
	private var _keyValueDes:String;
	
	public function URLUtils(url:String)
	{
		_url = url;
		
		init(); 
	}
	
	private function init():void
	{
		var separator:String = _url.indexOf("/") > -1 ? "/" : "\\";
		var fileNameDes:String = _url.split(separator).pop();
		
		//截去地址参数
		var wenHaoIndex:int = fileNameDes.indexOf("?");
		if (wenHaoIndex > -1)
		{
			_keyValueDes = fileNameDes.substring(wenHaoIndex + 1); 
			fileNameDes = fileNameDes.substring(0, wenHaoIndex);
		}
		
		var fileNameDesArr:Array = fileNameDes.split(".");
		_fileName = fileNameDesArr.shift();
		_fileType = fileNameDesArr.shift();
		fileNameDesArr = null;
	}
	
	/**
	 * 文件名
	 */
	public function get fileName():String
	{
		return _fileName;
	}
	
	/**
	 * 文件扩展名
	 */
	public function get fileType():String
	{
		return _fileType;
	}
	/**
	 * 返回以“&”间隔的地址参数
	 */
	public function get params():Vector.<KeyValue>
	{
		if (_keyValueDes == null)
		{
			return null;
		}
		
		var result:Vector.<KeyValue> = new Vector.<KeyValue>();
		var keyValueArr:Array = _keyValueDes.split("&");
		for (var i:int, l:int = keyValueArr.length; i < l; i ++)
		{
			var paramDes:String = keyValueArr[i];
			var paramDesArr:Array = paramDes.split("=");
			result.push(new KeyValue(paramDesArr[0], paramDesArr[1]));
		}
		return result;
	}
}
}
